<template>
  <div>
    <el-dialog :title="types=='add'?'新增':'修改'" :visible="isShow" :before-close="close" width="60%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色编号" prop="roleid">
          <el-select v-model="ruleForm.roleid" placeholder="请选择角色名称">
            <el-option :label="role.rolename" :value="role.id" v-for="(role,index) in roleOptions" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click.stop="close">取消</el-button>
        <el-button @click.stop="submit" type="primary">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, editUser } from '@/api/index'
export default {
  name: 'MyWin',
  props: ['isShow', 'winList', 'types'],
  data () {
    return {
      // 下拉的数据
      roleOptions: [],
      ruleForm: {
        roleid: 0,
        username: '',
        password: '',
        status: '1'
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close () {
      this.$emit('updates')
    },
    submit () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let res
          // eslint-disable-next-line
          if (this.types == 'add') {
            res = await addUser(this.ruleForm)
            // eslint-disable-next-line
            if (res.code == 200) {
              this.$emit('update')
            }
          } else {
            res = await editUser(this.ruleForm)
            // eslint-disable-next-line
            if (res.code == 200) {
              this.$emit('update')
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // 调用关闭函数
      this.close()
    },
    setVal (data) {
      // 给表单项赋值
      this.ruleForm = data
    },
    getRolelist () {
      this.$requests('/rolelist').then(res => {
        this.roleOptions = res.list
      })
    }
  },
  created () {
    // 发请求 获取下拉菜单的数据
    this.getRolelist()
  }
}
</script>

<style lang="less" scoped>
</style>
